/**
  ETFAna project, Anyang Normal University && IMP-CAS
  \class ETFGOnline
  \brief The main frame of the ETFAna online GUI
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2024-06-15
  \date 2024-06-15 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                   </tr>
  <tr>  <td>2024-06-15   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021-2024 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#ifndef ETFGOnline_h
#define ETFGOnline_h

#include <unordered_map>
#include <vector>
#include <list>
#include <string>
#include <TGFrame.h>
#include "ETFOnlineTask.h"

typedef ETFOnlineTask::hcmd_t hc_t;

using std::unordered_map;
using std::list;
using std::vector;
using std::string;

class TRootEmbeddedCanvas;
class TGButtonGroup;
class TGTextEntry;
class TGComboBox;
class TGListBox;
class TCanvas;
class TH1;

class ETFGOnline : public TGMainFrame{
public:
	ETFGOnline(int w = 800, int h = 600);
	virtual ~ETFGOnline();

  virtual void Initialize();
  virtual void Configure();
	virtual void DoClose();
  /// @{ ATOMIC ACTIONS
  virtual void Reset();
  virtual void ResetAll(); ///< reset all the histos
  virtual void Update(); ///< update the canvas
  virtual void SetDrawOption(const char *opt = "") override;
  virtual void SetCut();
  virtual void Draw(const char *opt = "") override;
  /// ATOMIC ACTIONS @}
  virtual void SetUpdateDT(); ///< set update interval from GUI input
  virtual void SelectHistGroup(int id); ///< select list group according to radio buttons
  virtual void SetCurrentHist(int id); ///< reaction to DoubleClick of a histogram
  virtual void DivideCanvas(); ///< fCanvas->Divide(fNx, fNy);
  virtual void AddHist(hc_t *h);
  virtual void ClearHists();
  virtual void UpdateConfig(); ///< update according to online.yaml
  virtual int UpdateDTInMS() const{ return fUpdateDT; }
  void SetOnlineTask(ETFOnlineTask *l){ fOnlineTask = l; }
  const unordered_map<string, int> &HistGroup() const{ return fHistGroup; }

  ClassDefOverride(ETFGOnline, 0);

protected:
  vector<string> fBtnGrps;
  TCanvas *fCanvas;
  TGTextEntry *fUpdateDTInput, *fCutInput, *fDrawOptInput;
  TGComboBox *fCanDivide; ///< select divisions of the canvas
  TGButtonGroup *fRadGroup;
  TGListBox *fHistListBox;
  unordered_map<string, int> fHistGroup; ///< map<grp_name, id>
  vector<list<hc_t *> *> fHistMat; ///< fHistMat[grp][id]
  list<hc_t *> *fHistList0; ///< the current hist list
  list<hc_t *>::iterator fIter; ///< points to the first hist in the canvas

  int fUpdateDT; ///< update time interval in ms
  int fNx, fNy; ///< division of the canvas
  string fDrawOpt, fCut; ///< fCut: temporarily not used

  ETFOnlineTask *fOnlineTask;
};

#endif
